/**
 * 1. 旋转的目标位置
 * 2. 速度
 */

/**
 * 最后 N个 不再与之前的关联
 * 此外 这个 N个的角度覆盖的范围，不再 正常的范围内
 * 目标速度递减到0
 * 并且没经过一个发射一个 自定义事件，用于自定义处理
 */
var math = require("../../../activities/zodiacs/zodiacsMath");
cc.Class({
    extends: cc.Component,

    properties: {
        time: {
            default: 0,
            displayName: "总时间",
            min: 0,
            step: 1,
        },
        turnLap: {
            default: 0,
            displayName: "转动圈数",
            min: 0,
            step: 1,
        },
        eventName: '',
    },

    startDraw(targets) {
        let extraAngle = 0;
        this._normalAngle = this.turnLap * 360;
        var ends = cc.js.mixin([], this.getLastAngles(targets));
        let len = targets.length;
        this._targets = cc.js.mixin([], targets);
        this._targets.length = 0;
        this._targets = [];

        for (let i = 0; i < len; i++) {
            this._targets.push({
                num: targets[i],
                valid: true,
            });
        }
        extraAngle = ends[ends.length - 1];

        this._starting = true;
        this._curSpeed = 0;
        this._stepNo = 0;
        this._turningAngle = 0;
        this._totalAngle = this._normalAngle + extraAngle;
        var rotateAni = cc.rotateTo(this.time, this._totalAngle).easing(cc.easeInOut(3.0)); //停止状态 慢 - 快 - 慢;
        var self = this;

        rotateAni.update = function (dt) {
            dt = this._computeEaseTime(dt);
            if (this.target) {
                this.target.rotationX = this._startAngleX + this._diffAngleX * dt;
                this.target.rotationY = this._startAngleY + this._diffAngleY * dt;

                self.checkEndsPass(this.target.rotationX, this._diffAngleX);
            }
        };
        var callFunc = cc.callFunc(() => {
            bp.event.emit(this.eventName, -1);
        });

        this.node.runAction(cc.sequence(rotateAni, callFunc));
    },

    checkEndsPass(rotationX, dstX) {
        let copyAngle = math.getAngleByIndex(1) * 0.5;
        let len = this._targets.length;
        let startAngle;
        let endAngle;
        for (let i = len - 1; i > -1; i--) {
            startAngle = dstX + copyAngle * (i * 2 - 5);
            endAngle = startAngle + 2 * copyAngle;
            if (i == len - 1) {
                startAngle += copyAngle * 0.8;
            } else {
                startAngle += copyAngle;
            }
            if (this._targets[i].valid && ((rotationX > startAngle && rotationX < endAngle) || (rotationX >= endAngle))) {
                let index = this._targets[i].num;
                this._targets[i].valid = false;
                bp.event.emit(this.eventName, index);
            }
        }
    },

    getLastAngles(targets) {
        let len = targets.length;
        let angles = [];
        let startAngle = -1;
        for (let i = 0; i < len; i++) {
            if (startAngle < 0) {
                startAngle = math.getAngleByIndex(targets[0]);
            } else {
                startAngle += math.getAngleByIndex(1);
            }
            angles.push(startAngle);
        }
        return angles;
    },
});
